package com.semnox.androidgame.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.semnox.androidgame.test.image.Image;
import com.semnox.androidgame.test.image.component.Speed;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback{

	private static final String TAG = MainGamePanel.class.getSimpleName();

	private MainThread thread;
	private Image image;

	private String avgFps;
	
	public MainGamePanel(Context context) {
		super (context);
		//adding the callback (this) to surface holder to intercept events
		getHolder().addCallback(this);
		//create image and load bitmap
		image = new Image(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);
		//create game loop thread
		thread = new MainThread(getHolder(), this);
		//make the game panel focusable so it can handle events
		setFocusable(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		//at this point surface is created and we start the loop
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "Surface is being destroyed");
		//tell the thread to shut down and wait for it to finish
		//this is a clean shutdown
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				//try again shutting down the thread
			}
		}
		Log.d(TAG, "Thread was shutdown cleanly");
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			//delegating event handling to the image
			image.handleActionDown((int)event.getX(), (int)event.getY());
			//check if lower part of screen the we exit
			if(event.getY()>getHeight() - 50) {
				thread.setRunning(false);
				((Activity)getContext()).finish();
			} else {
				Log.d(TAG, "Coords: x=" +event.getX() + ",y=" + event.getY());
			}
		} if (event.getAction() == MotionEvent.ACTION_MOVE) {
			//the gestures
			if (image.isTouched()) {
				//image was picked up and dragged
				image.setX((int)event.getX());
				image.setY((int)event.getY());
			}
		} if (event.getAction() == MotionEvent.ACTION_UP) {
			//touch was released
			if (image.isTouched()) {
				image.setTouched(false);
			}
		}
		return true;		
	}
	
	public void setAvgFps(String avgFps) {
		this.avgFps=avgFps;
	}

	private void displayFps(Canvas canvas, String fps) {
		if (canvas!=null&&fps!=null) {
			Paint paint =new Paint();
			paint.setARGB(255, 255, 255, 255);
			canvas.drawText(fps, this.getWidth()-50, 20, paint);
		}
	}
	
	protected void render(Canvas canvas){
		//fill the canvas with black color
		canvas.drawColor(Color.BLACK);
		image.draw(canvas);
		//display fps
		displayFps(canvas, avgFps);
	}

	public void update() {
		//check collision with right wall if heading right
		if (image.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT && image.getX() + image.getBitmap().getWidth()/2>=getWidth()) {
			image.getSpeed().toggleXDirection();
		}
		//check collision with left wall if heading left
		if (image.getSpeed().getxDirection() == Speed.DIRECTION_LEFT && image.getX() - image.getBitmap().getWidth()/2<=0) {
			image.getSpeed().toggleXDirection();
		}
		//check collision with bottom wall if heading down
		if (image.getSpeed().getyDirection() == Speed.DIRECTION_DOWN && image.getY() + image.getBitmap().getHeight()/2>=getHeight()) {
			image.getSpeed().toggleYDirection();
		}
		//check collision with top wall if heading up
		if (image.getSpeed().getyDirection() == Speed.DIRECTION_UP && image.getY() - image.getBitmap().getHeight()/2<=0) {
			image.getSpeed().toggleYDirection();
		}
		image.update();
	}
}
